/////////////////////////////////////////////////////////////////////////////
// Name:        splashframe.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Mon May 14 21:45:58 2007
// RCS-ID:      
// Copyright:   Copyright (c) daodict.com 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/wxsqlite3.h>

#include "splashframe.h"
#include "thememgr.h"
#include "dictmgr.h"
#include "cfgmgr.h"
#include "component/splashscreen.h"
#include "mainframe.h"
#include "mainnb.h"
#include "notebooks/wordswnd.h"
#include "application.h"

/**
 * SplashFrame constructors
 */
SplashFrame::SplashFrame() : wxFrame(NULL, ID_SPLASH_FRAME, _T("Daodict Splash"), \
                                 wxDefaultPosition, wxDefaultSize, \
                                 wxSIMPLE_BORDER 
                                 ) {
	p_main_frame = NULL; // must!
	
	wxLogMessage(_T("SplashFrame::SplashFrame"));
    
    p_main_sizer = new wxBoxSizer(wxVERTICAL);
    this->SetSizer(p_main_sizer);
    
	wxBitmap bmp = ThemeManager::get().get_bitmap(_T("splash.png"));
    p_bmp_wnd = new SplashScreenWindow(bmp, this, ID_SPLASH_SCREEN, wxDefaultPosition, wxSize(432,324));
    p_main_sizer->Add(p_bmp_wnd, 1, wxGROW, 0);
    
    //wxBoxSizer* p_loading_sizer = new wxBoxSizer(wxVERTICAL);
    //p_bmp_wnd->SetSizer(p_loading_sizer);
    
    //wxBoxSizer* p_temp_sizer = new wxBoxSizer(wxVERTICAL);
    //p_loading_sizer->Add(p_temp_sizer, 1, wxGROW, 0);
    
    p_loading_stc = new wxStaticText(this, wxID_STATIC, _T("Loading ... "));
    p_loading_stc->SetBackgroundColour(*wxWHITE);
    p_main_sizer->Add(p_loading_stc, 0, wxGROW, 0);
    
    p_loading_gauge = new wxGauge(this, ID_LOADING_GAUGE, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL|wxGA_SMOOTH );
    p_loading_gauge->SetValue(1);
    p_main_sizer->Add(p_loading_gauge, 0, wxGROW, 0);
    
    Fit();
    CentreOnScreen();
    Layout();
    
    InitEvents();

	
	_update_timer.SetOwner(this, ID_UPDATE_TIMER);
	_update_timer.Start(100);
	_loading_timer.SetOwner(this, ID_LOADING_TIMER);
	_loading_timer.Start(10);
}


/**
 * SplashFrame destructor
 */
SplashFrame::~SplashFrame() {
	wxLogMessage(_T("SplashFrame::~SplashFrame"));
////@begin MainFrame destruction
////@end MainFrame destruction
}

void SplashFrame::InitEvents() {
	this->Connect(ID_UPDATE_TIMER, wxEVT_TIMER, 
			(wxObjectEventFunction)&SplashFrame::OnUpdateTimer);
	this->Connect(ID_LOADING_TIMER, wxEVT_TIMER, 
			(wxObjectEventFunction)&SplashFrame::OnLoadingTimer);
	this->Connect(wxEVT_CLOSE_WINDOW, (wxObjectEventFunction)&SplashFrame::OnCloseWnd);
}

void SplashFrame::OnUpdateTimer(wxTimerEvent& event) {
	//wxLogMessage(_T("OnUpdateTimer======================"));
	_update();
}

void SplashFrame::OnLoadingTimer(wxTimerEvent& event) {
	_loading_timer.Stop();
	_update();

	_inc_progress(_T("Initializing dictionaries ..."), 10);
	
	_find_new_or_del_dicts();
	
	_inc_progress(_T("Loading main window ..."), 40);

	ConfigManager& cfgmgr = ConfigManager::get();
	p_main_frame = new MainFrame(cfgmgr.Read(_T("/Window/MainFrame/width"), 750), \
			cfgmgr.Read(_T("/Window/MainFrame/height"), 550));
	// ::wxGetApp().SetTopWindow(p_main_frame);
	::wxGetApp().SetExitOnFrameDelete(true);
	
	// this->Close(true);
	this->Hide();
	
	p_main_frame->Show(true);
	p_main_frame->relayout();
}

void SplashFrame::OnCloseWnd(wxCloseEvent& event) {
	p_main_frame->Destroy();
	event.Skip();
}


void SplashFrame::_find_new_or_del_dicts() {
	_update();
	
	DictManager& dictmgr = DictManager::get();
	
    wxArrayString dictfn_array;
    wxDir::GetAllFiles(dictmgr.dict_dir.GetPath(),&dictfn_array,_T("*.dict"),wxDIR_FILES);

    // uninstall deleted dicts =========================================
    int count = 20/(dictmgr.installed_dict_count+1);
    
    _inc_progress(_T("Find deleted dictionaries ..."), count);
    
    wxArrayString& filenames = dictmgr.installed_dict_filenames;
    
    for (int i=0; i<dictmgr.installed_dict_count; i++) {
    	//wxMessageBox(wxString::Format(_T("find: %s, %d"), filenames[i].c_str(), i));
	    _inc_progress(_T("Find deleted dictionaries ..."), count);
    	
    	dictmgr.dict_dir.SetFullName(filenames[i]);
    	
    	// uninstall deleted dicts
    	if (not dictmgr.dict_dir.FileExists()) {
        	//wxMessageBox(wxString::Format(_T("uninstall: %s, %d"), filenames[i].c_str(), i));
    		_inc_progress(wxString::Format(_T("Uninstall dictionary: %s ..."), filenames[i].c_str()),0);
	    	
    		dictmgr.uninstall_dict(filenames[i]);
    	}

    	dictfn_array.Remove(dictmgr.dict_dir.GetFullPath());
    }
    // end uninstall deleted dicts =========================================

	count = dictfn_array.GetCount();
	int pgress = 30/(count+1);
	_inc_progress(_T("Find added dictionaries ..."), pgress);

    // install new added dicts =======================================
    for (int i=0; i<count; i++) {
    	wxString filename = wxFileName(dictfn_array[i]).GetFullName();
    	//wxMessageBox(filename);
    	
    	_inc_progress(wxString::Format(_T("Install dictionary %s ..."), filename.c_str()), pgress);
    	
    	//wxMessageBox(filename);
    	dictmgr.uninstall_dict(filename);
    	//wxMessageBox(filename);
    	dictmgr.install_dict(filename);
    	//wxMessageBox(filename);
    }
    // endinstall new added dicts =======================================

    dictmgr.maintains_mgrdb();
    dictmgr.reinit_installed_dict_info();
    dictmgr.create_all_dicts_dbconn();
}


void SplashFrame::_update() {
	Update();
	p_bmp_wnd->Refresh();
}


void SplashFrame::_inc_progress(const wxString& msg, int inc) {
	p_loading_stc->SetLabel(msg);
	p_loading_gauge->SetValue(p_loading_gauge->GetValue()+inc);
	_update();
}

